package net.pimpas.web.jsf.components.validators;

import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProvider;
import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProviderFactoryImpl;

public abstract class PimpasValidator implements Validator, StateHolder {
	
	private Class bundleHandler;
	
	private static final ResourceBundleServiceProvider bundle = 
		ResourceBundleServiceProviderFactoryImpl.getResourceBundleServiceProvider();
	

	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		
		this.validateComponent(context, component, value);
	}
	
	public ResourceBundleServiceProvider getBundle( ) {
		return bundle;
	}
	
	protected abstract void validateComponent(FacesContext context, UIComponent component, Object value)
	throws ValidatorException;

	public boolean isTransient() {
		return false;
	}

	public Object saveState(FacesContext context) {
		Object[] ob = new Object[1];
		ob[0] = this.getBundleHandler();
		
		return ob;
	}

	public void restoreState(FacesContext context, Object state) {
		Object[] ob = (Object[]) state;
		
		this.setBundleHandler((Class) ob[0]);
	}

	public void setTransient(boolean arg0) {
		
	}

	public Class getBundleHandler() {
		return bundleHandler;
	}

	public void setBundleHandler(Class bundleHandler) {
		this.bundleHandler = bundleHandler;
	}
	
}